package com.cal.utms.action.maintenance;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.cal.utms.dao.maintenance.CustodianContactPersonDao;
import com.cal.utms.dao.maintenance.CustodianDao;
import com.cal.utms.dao.maintenance.FundInstrumentMapDao;
import com.cal.utms.dao.maintenance.IssuerDaoImpl;
import com.cal.utms.dao.maintenance.NewFundDao;
import com.cal.utms.dao.user_maintenance.UserDaoImpl;
import com.cal.utms.dto.maintenance.NewFundDto;
import com.cal.utms.entity.CustodianContactPerson;
import com.cal.utms.entity.Fund;
import com.cal.utms.entity.FundInstrumentMap;
import com.cal.utms.entity.Issuer;
import com.cal.utms.model.maintenance.NewFundModel;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class NewFundAction extends ActionSupport implements ModelDriven<NewFundDto> {

	private static final long serialVersionUID = 1L;
	private NewFundDto newFundDto = new NewFundDto();

	// private String contactPersons;

	@Override
	public NewFundDto getModel() {
		return newFundDto;
	}

	public String getFundWithUnitValue() {
		NewFundModel newFundModel = new NewFundModel();
		newFundDto.setFundDtos(newFundModel.getFundWithUnitValue());
		return Action.SUCCESS;
	}

	public String getCustodianList() {
		CustodianDao custD = new CustodianDao();
		newFundDto.setCustodianList(custD.getList());
		return Action.SUCCESS;
	}

	public String getFundManagerList() {
		UserDaoImpl udi = new UserDaoImpl();
		newFundDto.setFundManagerList(udi.getFundManagerList());
		return Action.SUCCESS;
	}

	// Modified by Malsha
	public String getIssuerList() {
		IssuerDaoImpl issuerDao = new IssuerDaoImpl();
		newFundDto.setIssuerList(issuerDao.getAllIssuers());
		return Action.SUCCESS;
	}

	// Modified by Malsha
	public String getFdIssuerList() {
		IssuerDaoImpl issuerDaoImpl = new IssuerDaoImpl();
		List<Issuer> issuers = issuerDaoImpl.getIssuerByFundId(newFundDto.getId());
		newFundDto.setIssuerList(issuers);
		return Action.SUCCESS;
	}

	public String addFund() {
		NewFundModel newFundModel = new NewFundModel();
		NewFundDao newFundDao = new NewFundDao();
		Fund fund = null;

		System.out.println("Adding the fund: " + newFundDto.getName() + " : " + newFundDto.getName());
		fund = newFundModel.createEntity(newFundDto);
		// newFundDao.add(fund);
		newFundDao.add(fund);
		System.out.println("Fund Saved");

		newFundDto.setSuccess(true);
		return Action.SUCCESS;
	}

	public String confirmFund() {
		NewFundModel newFundModel = new NewFundModel();
		NewFundDao newFundDao = new NewFundDao();
		CustodianContactPersonDao ccpDao = new CustodianContactPersonDao();
		FundInstrumentMapDao fimDao = new FundInstrumentMapDao();
		int fundId = 0;

		Fund fund = newFundDao.getByCode(newFundDto.getCode());
		if (fund == null)
			return Action.ERROR;
		fundId = fund.getId();

		List<CustodianContactPerson> oldCcpList = fund.getCustodianContactPersons();
		List<CustodianContactPerson> tempOldCcpList = new ArrayList<CustodianContactPerson>(oldCcpList);
		List<CustodianContactPerson> tempNewCcpList = null;
		List<FundInstrumentMap> oldFimList = fund.getFundInstrumentMaps();
		List<FundInstrumentMap> tempOldFimList = new ArrayList<FundInstrumentMap>(oldFimList);
		List<FundInstrumentMap> tempNewFimList = null;

		System.out.println("Updating the fund: " + newFundDto.getName() + " : " + newFundDto.getName());

		fund = newFundModel.confirmEntity(newFundDto, fund);
		tempNewCcpList = new ArrayList<CustodianContactPerson>(fund.getCustodianContactPersons());
		tempNewFimList = new ArrayList<FundInstrumentMap>(fund.getFundInstrumentMaps());

		fund.setId(fundId);
		fund.setPortfolio(new BigDecimal(0));
		fund.setUpdateDate(new Date());
		newFundDao.update(fund);
		System.out.println("Fund confirmed " + fundId);

		for (CustodianContactPerson ccp : fund.getCustodianContactPersons()) {
			tempOldCcpList.remove(ccp);
		}
		for (CustodianContactPerson ccp : oldCcpList) {
			tempNewCcpList.remove(ccp);
		}
		for (CustodianContactPerson ccp : tempOldCcpList) {
			ccpDao.delete(ccp);
		}
		for (CustodianContactPerson ccp : tempNewCcpList) {
			ccpDao.add(ccp);
		}
		tempOldCcpList = null;
		tempNewCcpList = null;
		oldCcpList = null;
		System.out.println("Custodian contact persons Saved");

		for (FundInstrumentMap fim : fund.getFundInstrumentMaps()) {
			tempOldFimList.remove(fim);
		}
		for (FundInstrumentMap fim : oldFimList) {
			tempNewFimList.remove(fim);
		}
		for (FundInstrumentMap fim : tempOldFimList) {
			fimDao.delete(fim);
		}
		for (FundInstrumentMap fim : tempNewFimList) {
			fimDao.add(fim);
		}
		tempOldFimList = null;
		tempNewFimList = null;
		oldFimList = null;
		System.out.println("FundInsMap Saved");

		newFundDto.setSuccess(true);
		return Action.SUCCESS;
	}

	public String updateFund() {
		NewFundModel newFundModel = new NewFundModel();
		NewFundDao newFundDao = new NewFundDao();
		CustodianContactPersonDao ccpDao = new CustodianContactPersonDao();
		FundInstrumentMapDao fimDao = new FundInstrumentMapDao();
		int fundId = 0;

		Fund fund = newFundDao.getByCode(newFundDto.getCode());
		if (fund == null)
			return Action.ERROR;
		fundId = fund.getId();

		List<CustodianContactPerson> oldCcpList = fund.getCustodianContactPersons();
		List<CustodianContactPerson> tempOldCcpList = new ArrayList<CustodianContactPerson>(oldCcpList);
		List<CustodianContactPerson> tempNewCcpList = null;
		List<FundInstrumentMap> oldFimList = fund.getFundInstrumentMaps();
		List<FundInstrumentMap> tempOldFimList = new ArrayList<FundInstrumentMap>(oldFimList);
		List<FundInstrumentMap> tempNewFimList = null;

		System.out.println("Updating the fund: " + newFundDto.getName() + " : " + newFundDto.getName());

		fund = newFundModel.updateEntity(newFundDto, fund);
		tempNewCcpList = new ArrayList<CustodianContactPerson>(fund.getCustodianContactPersons());
		tempNewFimList = new ArrayList<FundInstrumentMap>(fund.getFundInstrumentMaps());

		fund.setId(fundId);
		newFundDao.update(fund);
		System.out.println("Fund updated " + fundId);

		for (CustodianContactPerson ccp : fund.getCustodianContactPersons()) {
			tempOldCcpList.remove(ccp);
		}
		for (CustodianContactPerson ccp : oldCcpList) {
			tempNewCcpList.remove(ccp);
		}
		for (CustodianContactPerson ccp : tempOldCcpList) {
			ccpDao.delete(ccp);
		}
		for (CustodianContactPerson ccp : tempNewCcpList) {
			ccpDao.add(ccp);
		}
		tempOldCcpList = null;
		tempNewCcpList = null;
		oldCcpList = null;
		System.out.println("Custodian contact persons Saved");

		for (FundInstrumentMap fim : fund.getFundInstrumentMaps()) {
			tempOldFimList.remove(fim);
		}
		for (FundInstrumentMap fim : oldFimList) {
			tempNewFimList.remove(fim);
		}
		for (FundInstrumentMap fim : tempOldFimList) {
			fimDao.delete(fim);
		}
		for (FundInstrumentMap fim : tempNewFimList) {
			fimDao.add(fim);
		}
		tempOldFimList = null;
		tempNewFimList = null;
		oldFimList = null;
		System.out.println("FundInsMap Saved");

		newFundDto.setSuccess(true);
		return Action.SUCCESS;
	}

	public String getNameList() {
		NewFundDao newFundDao = new NewFundDao();
		if (newFundDto.getQuerry() != null && !newFundDto.getQuerry().equals("")) {
			System.out.println("Get fundlist starts with " + newFundDto.getQuerry());
			newFundDto.setFundList(newFundDao.getNames(newFundDto.getQuerry()));
		} else {
			System.out.println("Get fundlist");
			newFundDto.setFundList(newFundDao.getNames());
		}
		return Action.SUCCESS;
	}

	public String getListForEod() {
		NewFundDao newFundDao = new NewFundDao();

		System.out.println("Get fundlist for EOD");
		newFundDto.setFundList(newFundDao.getNamesForEod());

		return Action.SUCCESS;
	}

	public String getDealFunds() {
		NewFundDao newFundDao = new NewFundDao();
		List<Fund> fundList = newFundDao.getNamesForDeal(newFundDto.getQuerry(), newFundDto.getQuerryTwo());
		newFundDto.setFundList(fundList);
		return Action.SUCCESS;
	}

	public static void main(String[] args) {
		NewFundDao newFundDao = new NewFundDao();
		for (Fund fund : newFundDao.getNamesForDeal("PURCH", "20")) {
			System.out.println(fund.getFundName());
		}
	}

	public String getNamesForDeal() {
		NewFundDao newFundDao = new NewFundDao();
		newFundDto.setFundList(newFundDao.getNamesForDeal(newFundDto.getQuerry()));
		return Action.SUCCESS;
	}

	public String getFund() {
		NewFundDao newFundDao = new NewFundDao();
		// newFundDto.setFund(newFundDao.getById(newFundDto.getId()));
		NewFundDto fundDto = new NewFundModel().decodeEntity(newFundDao.getById(newFundDto.getId()));
		newFundDto.setData(fundDto);
		newFundDto.setSuccess(true);
		return Action.SUCCESS;
	}

	public NewFundDto getNewFundDto() {
		return newFundDto;
	}

	public void setNewFundDto(NewFundDto newFundDto) {
		this.newFundDto = newFundDto;
	}

	/*
	 * public void setContactPersons(String contactPersons) {
	 * this.contactPersons = contactPersons; }
	 */

	public String checkFundCode() {
		if (newFundDto.getQuerry() != null) {
			NewFundDao newFundDao = new NewFundDao();
			Fund fund = newFundDao.getByCode(newFundDto.getQuerry());
			if (fund == null) {
				newFundDto.setSuccess(true);
			} else {
				newFundDto.setSuccess(false);
			}
		} else {
			newFundDto.setSuccess(true);
		}
		return Action.SUCCESS;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

}
